﻿namespace BM.Tools.WebControls.MForm.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Xml.XPath;

    using Items;

    /// <summary>
    /// Group is an <see cref="Offspring" /> control that corresponds to a group of xml nodes.
    /// </summary>
    public class Group : OffspringsOwner
    {
        /// <summary>
        /// Creates a <see cref="GroupItem" /> object
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <param name="navigator">
        /// The navigator.
        /// </param>
        /// <returns>
        /// </returns>        
        protected override OffspringItem CreateItem(int index, XPathNavigator navigator)
        {
            return new GroupItem(this, index, navigator);
        }

        /// <summary>
        /// The Group offspring does not change the navigator context.
        /// </summary>
        internal override string XPathString
        {
            get
            {
                var dummyItem = new GroupItem(this, 0, null);
                var offspringsNames = new List<string>();
                foreach (Offspring offspring in dummyItem.Offsprings)
                {
                    offspringsNames.Add(offspring.XPathString);
                }

                return string.Concat("self::*[", String.Join(" | ", offspringsNames.ToArray()), "]");
            }
        }
    }
}
